#region Using

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
using BlogEngine.Core;
using App_Code.Controls;

#endregion

public partial class widgets_Quotations_widget : WidgetBase
{
    public override void LoadWidget()
    {
        StringDictionary settings = GetSettings();
        XmlDocument doc = new XmlDocument();

        if (settings["content"] != null)
            doc.InnerXml = settings["content"];

        XmlNodeList quotes = doc.SelectNodes("//quotes");

        if (quotes.Count == 0)
        {
            pnlQuote.Visible = false;
        }
        else
        {
            pnlQuote.Visible = true;
            #region display the newest quote
            //Quote newestQuote = null;
            //foreach (XmlNode node in quotes)
            //{
            //    Quote thisQuote = new Quote(node);
            //    if (newestQuote == null)
            //        newestQuote = new Quote(node);
            //    else
            //    {
            //        if (newestQuote.LastShown.HasValue)
            //        {
            //            if (thisQuote.LastShown.HasValue)
            //            {
            //                if (Convert.ToDateTime(thisQuote.LastShown) > Convert.ToDateTime(newestQuote.LastShown))
            //                    newestQuote = new Quote(node);
            //            }
            //        }
            //    }
            //}
            //DisplayQuote(newestQuote);
            //// Do we update it?
            //if (Convert.ToDateTime(newestQuote.LastShown) < DateTime.Today)
            //    UpdateQuoteDate(newestQuote);
            #endregion

            #region Display random quote
            int i = RandomNumber(0, quotes[0].ChildNodes.Count - 1);
            XmlNode node = quotes[0].ChildNodes[i];
            DisplayQuote(new Quote(node));
            #endregion

        }
    }

    private int RandomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max + 1);
    }

    private void DisplayQuote(Quote quote)
    {
        pnlQuote.Visible = true;
        lblQuotation.Text = quote.Quotation;
        lblQuotationAuthor.Text = quote.Author;
    }

    public override string Name
    {
        get { return "Quotations"; }
    }

    public override bool IsEditable
    {
        get { return true; }
    }

    /// <summary>
    /// Quote Class used for Quote of the Day Control
    /// </summary>
    class Quote
    {
        public Quote() { }

        public Quote(XmlNode node)
        {
            if (node.Attributes["id"] != null)
                ID = node.Attributes["id"].Value;
            if (node.Attributes["author"] != null)
                Author = node.Attributes["author"].Value;
            if (node.Attributes["quotation"] != null)
                Quotation = node.Attributes["quotation"].Value;
            if (node.Attributes["lastshown"] != null)
            {
                string dt = node.Attributes["lastshown"].Value;
                if (!string.IsNullOrEmpty(dt))
                    LastShown = Convert.ToDateTime(dt);
            }


        }

        private string _id = string.Empty;
        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _author = string.Empty;
        public string Author
        {
            get { return _author; }
            set { _author = value; }
        }

        private string _quotation = string.Empty;
        public string Quotation
        {
            get { return _quotation; }
            set { _quotation = value; }
        }

        private DateTime? _lastShown = null;
        public DateTime? LastShown
        {
            get { return _lastShown; }
            set { _lastShown = value; }
        }

    }
}
